home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / SizeRequirements.java < prev    next >
Text File  |  1998-06-30  |  13KB  |  345 lines

  1. /*
  2.  * @(#)SizeRequirements.java    1.13 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22.  
  23. import java.awt.*;
  24. import java.io.Serializable;
  25.  
  26. /**
  27.  * For the convenience of layout managers,
  28.  * calculates information about the size and position of components.
  29.  * All size and position calculation methods are class methods
  30.  * that take arrays of SizeRequirements as arguments.
  31.  * The SizeRequirements class supports two types of layout:
  32.  *
  33.  * <blockquote>
  34.  * <dl>
  35.  * <dt> tiled
  36.  * <dd> The components are placed end-to-end,
  37.  *      starting at coordinate 0
  38.  *      (the leftmost or topmost position).
  39.  *
  40.  * <dt> aligned
  41.  * <dd> The components are aligned as specified 
  42.  *      by each component's X or Y alignment value.
  43.  * </dl>
  44.  * </blockquote>
  45.  *
  46.  * <p>
  47.  *
  48.  * Each SizeRequirements object contains information
  49.  * about either the width (and X alignment)
  50.  * or height (and Y alignment)
  51.  * of a single component or a group of components:
  52.  * 
  53.  * <blockquote>
  54.  * <dl>
  55.  * <dt> <code>minimum</code>
  56.  * <dd> The smallest reasonable width/height of the component
  57.  *      or component group, in pixels.
  58.  *
  59.  * <dt> <code>preferred</code>
  60.  * <dd> The natural width/height of the component
  61.  *      or component group, in pixels.
  62.  *
  63.  * <dt> <code>maximum</code>
  64.  * <dd> The largest reasonable width/height of the component
  65.  *      or component group, in pixels.
  66.  *
  67.  * <dt> <code>alignment</code>
  68.  * <dd> The X/Y alignment of the component
  69.  *      or component group.
  70.  * </dl>
  71.  * </blockquote>
  72.  * <p>
  73.  * Warning: serialized objects of this class will not be compatible with
  74.  * future swing releases.  The current serialization support is appropriate 
  75.  * for short term storage or RMI between Swing1.0 applications.  It will
  76.  * not be possible to load serialized Swing1.0 objects with future releases
  77.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  78.  * baseline for the serialized form of Swing objects.
  79.  *
  80.  * @see Component#getMinimumSize
  81.  * @see Component#getPreferredSize
  82.  * @see Component#getMaximumSize
  83.  * @see Component#getAlignmentX
  84.  * @see Component#getAlignmentY
  85.  *
  86.  * @version 1.13 04/09/98
  87.  * @author Timothy Prinzing
  88.  */
  89. public class SizeRequirements implements Serializable {
  90.  
  91.     /** 
  92.      * The minimum size required. 
  93.      * For a component <code>comp</code>, this should be equal to either
  94.      * <code>comp.getMinimumSize().width</code> or
  95.      * <code>comp.getMinimumSize().height</code>.
  96.      */
  97.     public int minimum;
  98.  
  99.     /** 
  100.      * The preferred (natural) size. 
  101.      * For a component <code>comp</code>, this should be equal to either
  102.      * <code>comp.getPreferredSize().width</code> or
  103.      * <code>comp.getPreferredSize().height</code>.
  104.      */
  105.     public int preferred;
  106.  
  107.     /** 
  108.      * The maximum size allowed. 
  109.      * For a component <code>comp</code>, this should be equal to either
  110.      * <code>comp.getMaximumSize().width</code> or
  111.      * <code>comp.getMaximumSize().height</code>.
  112.      */
  113.     public int maximum;
  114.  
  115.     /** 
  116.      * The alignment, specified as a value between 0.0 and 1.0,
  117.      * inclusive.
  118.      * To specify centering, the alignment should be 0.5.
  119.      */
  120.     public float alignment;
  121.  
  122.     /**
  123.      * Creates a SizeRequirements object with the minimum, preferred,
  124.      * and maximum sizes set to zero and an alignment value of 0.5
  125.      * (centered).
  126.      */
  127.     public SizeRequirements() {
  128.     minimum = 0;
  129.     preferred = 0;
  130.     maximum = 0;
  131.     alignment = 0.5f;
  132.     }
  133.  
  134.     /**
  135.      * Creates a SizeRequirements object with the specified minimum, preferred,
  136.      * and maximum sizes and the specified alignment.
  137.      *
  138.      * @param min the minimum size >= 0
  139.      * @param pref the preferred size >= 0
  140.      * @param max the maximum size >= 0
  141.      * @param a the alignment >= 0.0f && <= 1.0f
  142.      */
  143.     public SizeRequirements(int min, int pref, int max, float a) {
  144.     minimum = min;
  145.     preferred = pref;
  146.     maximum = max;
  147.     alignment = a > 1.0f ? 1.0f : a < 0.0f ? 0.0f : a;
  148.     }
  149.  
  150.     /**
  151.      * Returns a string describing the minimum, preferred, and maximum
  152.      * size requirements, along with the alignment.
  153.      *
  154.      * @return the string
  155.      */
  156.     public String toString() {
  157.     return "[" + minimum + "," + preferred + "," + maximum + "]@" + alignment;
  158.     }
  159.  
  160.     /**
  161.      * Determines the total space necessary to
  162.      * place a set of components end-to-end.  The needs
  163.      * of each component in the set are represented by an entry in the
  164.      * passed-in SizeRequirements array.
  165.      * The returned SizeRequirements object has an alignment of 0.5
  166.      * (centered).  The space requirement is never more than
  167.      * Integer.MAX_VALUE.
  168.      *
  169.      * @param children  the space requirements for a set of components.
  170.      *   The vector may be of zero length, which will result in a
  171.      *   default SizeRequirements object instance being passed back.
  172.      * @return  the total space requirements.
  173.      */
  174.     public static SizeRequirements getTiledSizeRequirements(SizeRequirements[] 
  175.                                 children) {
  176.     SizeRequirements total = new SizeRequirements();
  177.     for (int i = 0; i < children.length; i++) {
  178.         SizeRequirements req = children[i];
  179.         total.minimum = (int) Math.min((long) total.minimum + (long) req.minimum, Integer.MAX_VALUE);
  180.         total.preferred = (int) Math.min((long) total.preferred + (long) req.preferred, Integer.MAX_VALUE);
  181.         total.maximum = (int) Math.min((long) total.maximum + (long) req.maximum, Integer.MAX_VALUE);
  182.     }
  183.     return total;
  184.     }
  185.  
  186.     /**
  187.      * Determines the total space necessary to
  188.      * align a set of components.  The needs
  189.      * of each component in the set are represented by an entry in the
  190.      * passed-in SizeRequirements array.  The total space required will
  191.      * never be more than Integer.MAX_VALUE.
  192.      *
  193.      * @param children  the set of child requirements.  If of zero length,
  194.      *  the returns result will be a default instance of SizeRequirements.
  195.      * @return  the total space requirements.
  196.      */
  197.     public static SizeRequirements getAlignedSizeRequirements(SizeRequirements[] 
  198.                                   children) {
  199.     SizeRequirements totalAscent = new SizeRequirements();
  200.     SizeRequirements totalDescent = new SizeRequirements();
  201.     for (int i = 0; i < children.length; i++) {
  202.         SizeRequirements req = children[i];
  203.  
  204.         int ascent = (int) (req.alignment * req.minimum);
  205.         int descent = req.minimum - ascent;
  206.         totalAscent.minimum = Math.max(ascent, totalAscent.minimum);
  207.         totalDescent.minimum = Math.max(descent, totalDescent.minimum);
  208.  
  209.         ascent = (int) (req.alignment * req.preferred);
  210.         descent = req.preferred - ascent;
  211.         totalAscent.preferred = Math.max(ascent, totalAscent.preferred);
  212.         totalDescent.preferred = Math.max(descent, totalDescent.preferred);
  213.  
  214.         ascent = (int) (req.alignment * req.maximum);
  215.         descent = req.maximum - ascent;
  216.         totalAscent.maximum = Math.max(ascent, totalAscent.maximum);
  217.         totalDescent.maximum = Math.max(descent, totalDescent.maximum);
  218.     }
  219.     int min = (int) Math.min((long) totalAscent.minimum + (long) totalDescent.minimum, Integer.MAX_VALUE);
  220.     int pref = (int) Math.min((long) totalAscent.preferred + (long) totalDescent.preferred, Integer.MAX_VALUE);
  221.     int max = (int) Math.min((long) totalAscent.maximum + (long) totalDescent.maximum, Integer.MAX_VALUE);
  222.     float alignment = 0.0f;
  223.     if (min > 0) {
  224.         alignment = (float) totalAscent.minimum / min;
  225.         alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
  226.     }
  227.     return new SizeRequirements(min, pref, max, alignment);
  228.     }
  229.  
  230.     /**
  231.      * Creates a bunch of offset/span pairs representing how to
  232.      * lay out a set of components end-to-end.  
  233.      * This method requires that you specify
  234.      * the total amount of space to be allocated,
  235.      * the size requirements for each component to be placed
  236.      * (specified as an array of SizeRequirements), and
  237.      * the total size requirement of the set of components.
  238.      * You can get the total size requirement
  239.      * by invoking the getTiledSizeRequirements method.
  240.      * 
  241.      * @param allocated the total span to be allocated >= 0.
  242.      * @param total     the total of the children requests.
  243.      * @param children  the size requirements for each component.
  244.      * @param offsets   the offset from 0 for each child where 
  245.      *   the spans were allocated (determines placement of the span).
  246.      * @param spans     the span allocated for each child to make the
  247.      *   total target span.
  248.      */
  249.     public static void calculateTiledPositions(int allocated,
  250.                            SizeRequirements total,
  251.                                SizeRequirements[] children,
  252.                                int[] offsets,
  253.                            int[] spans) {
  254.     if (allocated > total.preferred) {
  255.         expandedTile(allocated, total, children, offsets, spans);
  256.     } else {
  257.         compressedTile(allocated, total, children, offsets, spans);
  258.     }
  259.     }
  260.  
  261.     /**
  262.      * Creates a bunch of offset/span pairs specifying how to
  263.      * lay out a set of components with the specified alignments.
  264.      * The resulting span allocations will overlap, with each one
  265.      * fitting as well as possible into the given total allocation.
  266.      * This method requires that you specify
  267.      * the total amount of space to be allocated,
  268.      * the size requirements for each component to be placed
  269.      * (specified as an array of SizeRequirements), and
  270.      * the total size requirements of the set of components
  271.      * (only the alignment field of which is actually used).
  272.      * You can get the total size requirement by invoking 
  273.      * getAlignedSizeRequirements.
  274.      * 
  275.      * @param allocated the total span to be allocated >= 0.
  276.      * @param total     the total of the children requests.
  277.      * @param children  the size requirements for each component.
  278.      * @param offsets   the offset from 0 for each child where 
  279.      *   the spans were allocated (determines placement of the span).
  280.      * @param spans     the span allocated for each child to make the
  281.      *   total target span.
  282.      */
  283.     public static void calculateAlignedPositions(int allocated,
  284.                                                  SizeRequirements total,
  285.                                  SizeRequirements[] children,
  286.                                  int[] offsets,
  287.                          int[] spans) {
  288.     int totalAscent = (int) (allocated * total.alignment);
  289.     int totalDescent = allocated - totalAscent;
  290.     for (int i = 0; i < children.length; i++) {
  291.         SizeRequirements req = children[i];
  292.         int maxAscent = (int) (req.maximum * req.alignment);
  293.         int maxDescent = req.maximum - maxAscent;
  294.         int ascent = Math.min(totalAscent, maxAscent);
  295.         int descent = Math.min(totalDescent, maxDescent);
  296.         
  297.         offsets[i] = totalAscent - ascent;
  298.         spans[i] = (int) Math.min((long) ascent + (long) descent, Integer.MAX_VALUE);
  299.     }
  300.     }
  301.  
  302.     private static void compressedTile(int allocated, SizeRequirements total,
  303.                        SizeRequirements[] request,
  304.                        int[] offsets, int[] spans) {
  305.  
  306.     // ---- determine what we have to work with ----
  307.     int totalPlay = Math.min(total.preferred - allocated,
  308.                  total.preferred - total.minimum);
  309.     float factor = (total.preferred - total.minimum == 0) ? 0.0f :
  310.         (float) totalPlay / (total.preferred - total.minimum);
  311.  
  312.     // ---- make the adjustments ----
  313.     int totalOffset = 0;
  314.     for (int i = 0; i < spans.length; i++) {
  315.         offsets[i] = totalOffset;
  316.         SizeRequirements req = request[i];
  317.         int play = (int)(factor * (req.preferred - req.minimum));
  318.         spans[i] = req.preferred - play;
  319.         totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
  320.     }
  321.     }
  322.  
  323.     private static void expandedTile(int allocated, SizeRequirements total,
  324.               SizeRequirements[] request,
  325.               int[] offsets, int[] spans) {
  326.  
  327.     // ---- determine what we have to work with ----
  328.     int totalPlay = Math.min(allocated - total.preferred,
  329.                  total.maximum - total.preferred);
  330.     float factor = (total.maximum - total.preferred == 0) ? 0.0f :
  331.         (float) totalPlay / (total.maximum - total.preferred);
  332.  
  333.     // ---- make the adjustments ----
  334.     int totalOffset = 0;
  335.     for (int i = 0; i < spans.length; i++) {
  336.         offsets[i] = totalOffset;
  337.         SizeRequirements req = request[i];
  338.         int play = (int)(factor * (req.maximum - req.preferred));
  339.         spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE);
  340.         totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
  341.     }
  342.     }
  343.  
  344. }
  345.